package com.smartHome.config;

import com.smartHome.interceptors.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Properties;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private LoginInterceptor loginInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //登录和注册接口不拦截
        registry.addInterceptor(loginInterceptor).excludePathPatterns("/user/login","/user/register","/user/register/*","/email/getCode/*","/upload");
    }

    @Bean
    public JavaMailSender javaMailSender() {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();

        // 配置邮件服务器信息
        mailSender.setHost("smtp.qq.com");
        mailSender.setPort(465); // 465端口为SSL端口
        mailSender.setUsername("1992515303@qq.com");
        mailSender.setPassword("mmkefuoxdyxlbcjj");

        // 配置邮件传输协议
        Properties javaMailProperties = new Properties();
        javaMailProperties.put("mail.smtp.auth", "true");  // 启用SMTP身份验证
        javaMailProperties.put("mail.smtp.ssl.enable", "true"); // 启用SSL
        javaMailProperties.put("mail.smtp.starttls.enable", "false"); // 禁用STARTTLS，如果用的是SSL可以禁用此项
        mailSender.setJavaMailProperties(javaMailProperties);

        return mailSender;
    }
}
